import * as types from './actionTypes';
import Util from '../common/utils';

export let fetchCity = () => {

    return dispatch => {
        // 请求轮播数据
        dispatch(fetchCityInfo());
        return Util.getStorage('city', (res) => {
            dispatch(receiveCityInfo(res));
        }, (err) => {
            dispatch(receiveCityInfo({'city' : '沈阳'}));
        })
    }
};

export let fetchCityInfo = () => {
	return {
		'type' : types.FETCH_CITY_INFO,
	}
};

export let receiveCityInfo = (city) => {
	return {
		'type' : types.RECEIVE_CITY_INFO,
		'city' : city
	}
};



//从本地存储获得用户登录之后返回的userid和accessid
export let getUserLoginInfo = () => {

    return dispatch => {
        // 请求轮播数据
        dispatch(beginGetUserLoginInfo());
        return Util.getStorage('userlogininfo', (res) => {
            dispatch(receiveUserLoginInfo(res));
        }, (err) => {
            dispatch(receiveUserLoginInfo({}));
        })
    }
};

export let beginGetUserLoginInfo = () => {
    return {
        'type' : types.FETCH_USER_LOGININFO,
        'getting' : true,
    }
};

export let receiveUserLoginInfo = (userlogininfo) => {
    return {
        'type' : types.RECEIVE_USERID_LOGININFO,
        'userlogininfo' : userlogininfo,
        'getting' : false,
    }
};



//获取用户信息
export let getUserInfo = () => {
    let url = global.U + '/api/uc/uc/userService/getUserInfoById';

    return dispatch => {
        Util.getStorage('userlogininfo', (res) => {
            console.log(res);
            dispatch(beginGetUserInfo());
            return Util.post(url, {'userid': res.userId}, (res1) => {
                console.log('用户信息');
                console.log(res1);
                dispatch(receiveUserInfo(res1.data));
            }, (err) => {
                console.log(err);
                dispatch(receiveUserInfo({}));
            });
        }, (err) => {
            console.log(err);
            dispatch(receiveUserInfo({}));
        })
    };
}


export let beginGetUserInfo = () => {
    return {
        'type' : types.FETCH_USER_INFO,
        'getting' : true,
    }
}


export let receiveUserInfo = (userinfo) => {
    return {
        'type' : types.RECEIVE_USER_INFO,
        'userinfo' : userinfo,
        'getting' : false,
    }
}


